package com.ruoyi.business.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.ProjectDetailTemplate;
import com.ruoyi.business.mapper.ProjectDetailTemplateMapper;
import com.ruoyi.business.service.ProjectDetailTemplateService;
import com.ruoyi.business.domain.vo.ProjectDetailTemplateVO;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

import static com.ruoyi.business.domain.table.BankCardTableDef.BANK_CARD;
import static com.ruoyi.business.domain.table.ProjectDetailTemplateTableDef.PROJECT_DETAIL_TEMPLATE;

/**
 * 服务实现 : 个人项目模板
 *
 * @Author : ljq
 * @Created : 2025/10/8
 */
@Service
public class ProjectDetailTemplateServiceImpl extends ServiceImpl<ProjectDetailTemplateMapper, ProjectDetailTemplate> implements ProjectDetailTemplateService {

    @Override
    public Page<ProjectDetailTemplate> listByCondition(ProjectDetailTemplate entity, PageDomain domain) {
        QueryChain<ProjectDetailTemplate> queryChain = QueryChain.of(this.getMapper())
                .select(PROJECT_DETAIL_TEMPLATE.DEFAULT_COLUMNS)
                .from(PROJECT_DETAIL_TEMPLATE).as("t1");
        this.baseCondition(queryChain, entity);
        return queryChain
                .withRelations()
                .pageAs(new Page<>(domain.getPageNum(), domain.getPageSize()), ProjectDetailTemplate.class);
    }

    @Override
    public List<ProjectDetailTemplateVO> listByIds(List<Long> ids) {
        List<ProjectDetailTemplateVO> list = QueryChain.of(this.getMapper())
                .select(PROJECT_DETAIL_TEMPLATE.DEFAULT_COLUMNS)
                .from(PROJECT_DETAIL_TEMPLATE).as("t1")
                .leftJoin(BANK_CARD).as("t2").on(PROJECT_DETAIL_TEMPLATE.USER_ID.eq(BANK_CARD.USER_ID))
                .where(PROJECT_DETAIL_TEMPLATE.ID.in(ids))
                .withRelations()
                ///  忽略关联 .ignoreRelations("sysRoles")
                .listAs(ProjectDetailTemplateVO.class);

        return list;

    }


    /* ------------------- 基础查询条件 --------------------- */
    private QueryChain baseCondition(QueryChain chain, ProjectDetailTemplate entity) {
        // 项目关联ID
        if (StringUtils.isNotEmpty(entity.getProjectId())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.PROJECT_ID.eq(entity.getProjectId()));
        }
        // 用户关联ID
        if (StringUtils.isNotEmpty(entity.getUserId())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.USER_ID.eq(entity.getUserId()));
        }
        // 银行卡关联ID
        if (StringUtils.isNotEmpty(entity.getBankCardId())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.BANK_CARD_ID.eq(entity.getBankCardId()));
        }
        // 计划上班时间区间
        if (StringUtils.isNotEmpty(entity.getPlanWorkTimeRange())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.PLAN_WORK_TIME_RANGE.eq(entity.getPlanWorkTimeRange()));
        }
        // 备注
        if (StringUtils.isNotEmpty(entity.getComment())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.COMMENT.eq(entity.getComment()));
        }
        // 状态
        if (StringUtils.isNotEmpty(entity.getStatus())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.STATUS.eq(entity.getStatus()));
        }
        // 审批人
        if (StringUtils.isNotEmpty(entity.getApprovalBy())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.APPROVAL_BY.eq(entity.getApprovalBy()));
        }
        // 工种
        if (StringUtils.isNotEmpty(entity.getWorkType())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.WORK_TYPE.eq(entity.getWorkType()));
        }
        // 创建者
        if (StringUtils.isNotEmpty(entity.getCreateBy())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.CREATE_BY.eq(entity.getCreateBy()));
        }
        // 更新者
        if (StringUtils.isNotEmpty(entity.getUpdateBy())) {
            chain.where(PROJECT_DETAIL_TEMPLATE.UPDATE_BY.eq(entity.getUpdateBy()));
        }


        Map<String, Object> params = entity.getParams();

        // 实际上班时间-模糊匹配
        String realWorkStartLike = params.getOrDefault("realWorkStartLike", "").toString();
        if (StringUtils.isNotEmpty(realWorkStartLike)) {
            String[] split = realWorkStartLike.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.REAL_WORK_START_AT.like(realWorkStartLike));
        }

        // 实际下班时间-模糊匹配
        String realWorkEndAtLike = params.getOrDefault("realWorkEndAtLike", "").toString();
        if (StringUtils.isNotEmpty(realWorkEndAtLike)) {
            String[] split = realWorkEndAtLike.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.REAL_WORK_END_AT.like(realWorkEndAtLike));
        }

        // 创建时间
        String createTimeRange = params.getOrDefault("createTimeRange", "").toString();
        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] split = createTimeRange.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.CREATE_TIME.between(split[0], split[1]));
        }

        // 更新时间
        String updateTimeRange = params.getOrDefault("updateTimeRange", "").toString();
        if (StringUtils.isNotEmpty(updateTimeRange)) {
            String[] split = updateTimeRange.split(",");
            chain.where(PROJECT_DETAIL_TEMPLATE.UPDATE_TIME.between(split[0], split[1]));
        }

        // 项目关联ID-模糊匹配
        String projectIdLike = params.getOrDefault("projectIdLike", "").toString();
        if (StringUtils.isNotEmpty(projectIdLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.PROJECT_ID.like(projectIdLike));
        }

        // 用户关联ID-模糊匹配
        String userIdLike = params.getOrDefault("userIdLike", "").toString();
        if (StringUtils.isNotEmpty(userIdLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.USER_ID.like(userIdLike));
        }

        // 银行卡关联ID-模糊匹配
        String bankCardIdLike = params.getOrDefault("bankCardIdLike", "").toString();
        if (StringUtils.isNotEmpty(bankCardIdLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.BANK_CARD_ID.like(bankCardIdLike));
        }

        // 计划上班时间区间-模糊匹配
        String planWorkTimeRangeLike = params.getOrDefault("planWorkTimeRangeLike", "").toString();
        if (StringUtils.isNotEmpty(planWorkTimeRangeLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.PLAN_WORK_TIME_RANGE.like(planWorkTimeRangeLike));
        }

        // 备注-模糊匹配
        String commentLike = params.getOrDefault("commentLike", "").toString();
        if (StringUtils.isNotEmpty(commentLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.COMMENT.like(commentLike));
        }

        // 状态-模糊匹配
        String statusLike = params.getOrDefault("statusLike", "").toString();
        if (StringUtils.isNotEmpty(statusLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.STATUS.like(statusLike));
        }

        // 审批人-模糊匹配
        String approvalByLike = params.getOrDefault("approvalByLike", "").toString();
        if (StringUtils.isNotEmpty(approvalByLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.APPROVAL_BY.like(approvalByLike));
        }

        // 工种-模糊匹配
        String workTypeLike = params.getOrDefault("workTypeLike", "").toString();
        if (StringUtils.isNotEmpty(workTypeLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.WORK_TYPE.like(workTypeLike));
        }

        // 创建者-模糊匹配
        String createByLike = params.getOrDefault("createByLike", "").toString();
        if (StringUtils.isNotEmpty(createByLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.CREATE_BY.like(createByLike));
        }

        // 更新者-模糊匹配
        String updateByLike = params.getOrDefault("updateByLike", "").toString();
        if (StringUtils.isNotEmpty(updateByLike)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.UPDATE_BY.like(updateByLike));
        }

        // 项目关联ID-下拉框多选
        String projectIdList = params.getOrDefault("projectIdList", "").toString();
        if (StringUtils.isNotEmpty(projectIdList)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.PROJECT_ID.in(projectIdList.split(",")));
        }

        // 用户关联ID-下拉框多选
        String userIdList = params.getOrDefault("userIdList", "").toString();
        if (StringUtils.isNotEmpty(userIdList)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.USER_ID.in(userIdList.split(",")));
        }

        // 银行卡关联ID-下拉框多选
        String bankCardIdList = params.getOrDefault("bankCardIdList", "").toString();
        if (StringUtils.isNotEmpty(bankCardIdList)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.BANK_CARD_ID.in(bankCardIdList.split(",")));
        }

        // 状态-下拉框多选
        String statusList = params.getOrDefault("statusList", "").toString();
        if (StringUtils.isNotEmpty(statusList)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.STATUS.in(statusList.split(",")));
        }

        // 审批人-下拉框多选
        String approvalByList = params.getOrDefault("approvalByList", "").toString();
        if (StringUtils.isNotEmpty(approvalByList)) {
            chain.where(PROJECT_DETAIL_TEMPLATE.APPROVAL_BY.in(approvalByList.split(",")));
        }

        return chain;
    }
}